package com.zq.datastruc.tree;

import java.util.LinkedList;
import java.util.Queue;

public class BinarySearchTree<E> {

    private Node root;
    private int size;

    private Comparator<E> comparator;

    public BinarySearchTree() {
        this(null);
    }

    public BinarySearchTree(Comparator<E> comparator) {
        this.comparator = comparator;
    }


    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void clear() {
        root = null;
        size = 0;
    }

    public void preOrderTraversal() {
        preOrderTraversal(root);
    }

    private void preOrderTraversal(Node<E> node) {

        if (node == null) {
            return;
        }
        System.out.println(node.element);

        preOrderTraversal(node.left);
        preOrderTraversal(node.right);
    }

    public void inOrderTraversal() {
        inOrderTraversal(root);
    }

    private void inOrderTraversal(Node<E> node) {

        if (node == null) {
            return;
        }

        inOrderTraversal(node.left);
        System.out.println(node.element);
        inOrderTraversal(node.right);
    }

    public void postOrderTraversal() {
        postOrderTraversal(root);
    }

    private void postOrderTraversal(Node<E> node) {

        if (node == null) {
            return;
        }
        postOrderTraversal(node.left);
        postOrderTraversal(node.right);
        System.out.println(node.element);
    }

    //层序遍历
    public void levelOrderTraversal() {
        if (root == null) {
            return;
        }

        Queue<Node<E>> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            Node<E> node = queue.poll();
            System.out.println(node.element);

            if (node.left != null) {
                queue.offer(node.left);
            }

            if (node.right != null) {
                queue.offer(node.right);
            }
        }

    }

    public void add(E element) {

        elementNotNullCheck(element);

        //添加第一个节点
        if (root == null) {
            root = new Node(element, null);
            size++;
            return;
        }

        //添加的不是第一个节点
        //找到父节点
        Node<E> parent = null;
        Node<E> node = root;
        int cmp = 0;
        while (node != null) {
            cmp = compare(element, node.element);

            parent = node;

            if (cmp > 0) {
                node = node.right;
            } else if (cmp < 0) {
                node = node.left;
            } else {//相等
                node.element = element;
                return;
            }
        }

        //看看插入到父节点那个位置
        Node<E> newNode = new Node<>(element, parent);
        if (cmp > 0) {
            parent.right = newNode;
        } else {
            parent.left = newNode;
        }

        size++;


    }

    /**
     * 寻找前驱节点 ：中序遍历的前一个节点
     * 后继节点刚好与之相反
     *
     * @param node
     * @return
     */
    private Node<E> predecessor(Node<E> node) {

        //前驱节点在左子树当中则left.right.right.right
        Node<E> p = node.left;
        if (p != null) {
            while (p.right != null) {
                p = p.right;
            }
            //找到前驱节点
            return p;
        }

        while (node.parent != null && node == node.parent.left) {
            node = node.parent;
        }
        return node.parent;
    }

    private Node<E> afterdecessor(Node<E> node) {

        //前驱节点在左子树当中则right.left.left.left
        Node<E> p = node.right;
        if (p != null) {
            while (p.left != null) {
                p = p.left;
            }
            //找到前驱节点
            return p;
        }

        while (node.parent != null && node == node.parent.right) {
            node = node.parent;
        }
        return node.parent;
    }

    /**
     * 返回值0代表相等，大于0：e1>e2,小于0：e2>e1
     *
     * @return
     */
    public int compare(E e1, E e2) {
        if (comparator != null) {
            return comparator.compare(e1, e2);
        }

        return ((Comparable<E>) e1).compareTo(e2);
    }


    public void remove(E element) {

        remove(node(element));

    }

    private void remove(Node<E> node) {
        if (node == null) return;

        size--;

        //度为2
        if (node.hasTwochildren()) {
            //找到后继节点
            Node<E> s = afterdecessor(node);
            //用后继节点覆盖
            node.element = s.element;
            //删除后继节点
            node = s;
        }

        //度为1或者0
        Node<E> replacement = node.left != null ? node.left : node.right;

        if (replacement != null) {
            //度为1
            //维护子节点的父节点
            replacement.parent = node.parent;
            if (node.parent == null) {
                root = replacement;
            } else {
                if (node == node.parent.left) {
                    node.parent.left = replacement;
                } else {
                    node.parent.right = replacement;
                }
            }
        } else if (node.parent == null) {//根节点
            root = null;
        } else {//叶子节点
            if (node == node.parent.left) {
                node.parent.left = null;
            } else {
                node.parent.right = null;
            }
        }

    }

    private Node<E> node(E element) {
        Node<E> node = root;
        while (node != null) {
            int cmp = compare(element, node.element);
            if (cmp == 0) {
                return node;
            }
            if (cmp > 0) {
                node = node.right;
            } else {
                node = node.left;
            }
        }
        return null;
    }

    public boolean contains(E element) {
        return node(element)!=null;
    }


    private void elementNotNullCheck(E element) {
        if (element == null) {
            throw new IllegalArgumentException("element must not be null");
        }
    }


    private static class Node<E> {

        E element;
        Node<E> left;
        Node<E> right;
        Node<E> parent;

        public Node(E element, Node<E> parent) {
            this.element = element;
            this.parent = parent;
        }

        public boolean leafNode() {
            return left == null && right == null;
        }

        public boolean hasTwochildren() {
            return left != null && right != null;
        }
    }

}
